#include "qlineloading.h"
#include "qmath.h"
#include <QDebug>

QLineLoading::QLineLoading(QWidget *parent) : QWidget(parent)
{
    _maxDiameter = 8;
    _minDiameter = 5;
    _interval = 500;
    _index = 0;
    _count = 5;
    _dotColor = QColor(36, 167, 255);

    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    connect(&timer,&QTimer::timeout,this,&QLineLoading::refresh);

    this->hide();
}


void QLineLoading::setDotCount(int count)
{
    _count=count;
}


void QLineLoading::setDotColor(const QColor & color)
{
    _dotColor=color;
}


void QLineLoading::start()
{
    timer.setInterval(_interval);
    timer.start();
    this->show();
}

void QLineLoading::pause()
{
    timer.stop();
    this->hide();
}

void QLineLoading::setMaxDiameter(float max)
{
    _maxDiameter=max;
}

void QLineLoading::setMinDiameter(float min)
{
    _minDiameter=min;
}

void QLineLoading::refresh()
{
    repaint();
}

void QLineLoading::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)
    caculate();
}

void QLineLoading::caculate()
{
    locationList.clear();
    radiiList.clear();
    int width = this->width()/_count;
    for(int i=0;i<_count;i++) {
        if(i == 0)
            radiiList<<_maxDiameter;
        else
            radiiList<<_minDiameter;
        locationList.append(QPoint((i + 0.5)*width ,this->height()/2));
    }
}

void QLineLoading::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setPen(_dotColor);
    painter.setBrush(_dotColor);
    for(int i=0;i<_count;i++) {
        painter.setPen(_dotColor);
        float radii=radiiList.at((_index+_count-i)%_count);
        painter.drawEllipse(QPointF(locationList.at(i).x(),locationList.at(i).y()),radii,radii);
    }
    _index++;
}

